package org.example.banner.controller;

import org.example.entity.Base.Result;
import org.example.banner.entity.vo.Bannervo;
import org.example.banner.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Validated
@RestController
@RequestMapping("/banner")
public class BannerController {
    private final BannerService bannerService;

    public BannerController(BannerService bannerService) {
        this.bannerService = bannerService;
    }

    @GetMapping("/list")
    public Result List(){
        List<Bannervo> BannerList=bannerService.getList();
        if (BannerList==null){
            return Result.error("无图，查询失败");
        }
        return  Result.success(BannerList);
    }
    @GetMapping("/logo")
    public Result logo(){
        Bannervo bannervo=bannerService.logo();
        if (bannervo==null){
            return Result.error("查询失败");
        }
        return  Result.success(bannervo);
    }
    @GetMapping("/mainBanner")
    public Result MainBanner(){
        List<Bannervo> BannerList=bannerService.MainBanner();
        if (BannerList==null){
            return Result.error("无主页图，查询失败");
        }
        return  Result.success(BannerList);
    }
    @PostMapping("/addBanner")
    public Result addBanner(@RequestParam("image") MultipartFile image,
                            @RequestParam("name") String name,
                            @RequestParam("type")Integer type) throws Exception{
       if(bannerService.addBanner(image,name,type)==1)
          return  Result.success("新增成功");
        else return Result.error("新增失败");
    }
}
